<?php

class NewsController extends HBaseController
{
    //设置首页导航焦点
    public $active_cur = 'news';
    public $cur;

	public function actionIndex()
	{
		//推荐位
		$viewdata['news_positions'] = Positions::model()->getPosData('46');
		//推荐资讯
		$viewdata['news_all'] = News::model()->getNewsData('zne_focus=2 AND zne_status=2',7);
		//推荐专题
		$viewdata['subject_commend'] = News::model()->getNewsData('znc_id=9 AND zne_commend=2 AND zne_status=2',4);
		//专题列表
		$viewdata['subject'] = News::model()->getNewsData('znc_id=9 AND zne_commend=1 AND zne_status=2',3);
        //推荐精彩视频
		$video = NewsCat::model()->findByPk(6);
		$viewdata['video_commend'] = News::model()->getNewsData('znc_id in ('.$video->znc_childid.') AND zne_commend=2 AND zne_status=2',2);
        //精彩视频列表
		$viewdata['video'] = News::model()->getNewsData('znc_id in ('.$video->znc_childid.') AND zne_commend=1 AND zne_status=2',5);
        //图库
		$photo = NewsCat::model()->findByPk(7);
		$viewdata['photo'] = News::model()->getNewsData('znc_id in ('.$photo->znc_childid.') AND zne_commend=1 AND zne_status=2',5);
		
		$this->render('index', $viewdata);
	}


	//资讯分类列表,标签搜索
    public function actionList()
    {
		$id = $_GET['id'];
        if ($id) {
            $cat = NewsCat::model()->findByPk($id);
        }
		$tag = isset($_GET['tag'])?$_GET['tag']:"";
		$tags = Tag::model()->findByAttributes(array('zt_title'=>$tag));
		
		//分页
        $criteria = new CDbCriteria;
        $criteria->compare('znc_id', $id);
		//搜索关键词
		if($tag){
			$criteria->addcondition(" (zt_ids ='".$tags->zt_id."') or (zt_ids like '%,".$tags->zt_id."') or (zt_ids like '".$tags->zt_id.",%') or (zt_ids like '%,".$tags->zt_id.",%') ");
        }
		$criteria->compare('zne_status', '2');
        $dataProvider = new CActiveDataProvider('News', array(
            'criteria'=>$criteria,
            'pagination'=>array(
                'pageSize'=>5,
                'pageVar' => 'page',
            ),
            'sort' => array(
                'defaultOrder' => 'zne_id DESC'
            )
        ));
		
		//右栏 专题列表
        $subject = News::model()->getNewsData('znc_id=9 AND zne_commend=2 AND zne_status=2', 4);

        $this->render('list', array(
            'cat'=>$cat,
            'id'=>$id,
            'dataProvider'=>$dataProvider,
            'subject'=>$subject,
        ));
    }


    public function actionSpecial()
    {
        //设置二级导航焦点
        $this->cur = 'special';
        $id = 9;
        if ($id) {
            $cat = NewsCat::model()->findByPk($id);
        }
		
		//分页
        $criteria = new CDbCriteria;
        $criteria->compare('znc_id', $id);
        $criteria->compare('zne_status', '2');
        $dataProvider = new CActiveDataProvider('News', array(
            'criteria'=>$criteria,
            'pagination'=>array(
                'pageSize'=>8,
                'pageVar' => 'page',
            ),
            'sort' => array(
                'defaultOrder' => 'zne_id DESC'
            )
        ));
		
		//右栏 专题列表
        $subject = News::model()->getNewsData('znc_id=9 AND zne_commend=2 AND zne_status=2', 4);

        $this->render('special', array(
            'cat'=>$cat,
            'dataProvider'=>$dataProvider,
            'subject'=>$subject,
        ));
    }

    public function actionAlbum($sort='')
    {
        //设置二级导航焦点
        $this->cur = 'album';
        $id = 7;
        if ($id) {
            $cat = NewsCat::model()->findByPk($id);
        }
		$cat_c= explode(',',$cat->znc_childid);
		//分页
        $criteria = new CDbCriteria;
        $criteria->compare('znc_id',$cat_c);
        $criteria->compare('zne_status', '2');
		($sort != '')?$criteria->order = 'zne_hits DESC':'';
        $dataProvider = new CActiveDataProvider('News', array(
            'criteria'=>$criteria,
            'pagination'=>array(
                'pageSize'=>20,
                'pageVar' => 'page',
            ),
            'sort' => array(
                'defaultOrder' => 'inputtime DESC'
            )
        ));

        $this->render('album', array(
            'cat'=>$cat,
            'dataProvider'=>$dataProvider,
        ));
    }
	
	public function actionVideo()
	{
		 //设置二级导航焦点
        $this->cur = 'video';
        $id = 6;
        if ($id) {
            $cat = NewsCat::model()->findByPk($id);
        }

		//推荐位
		$video_focus = News::model()->findByAttributes(array('znc_id'=> explode(',',$cat->znc_childid), 'zne_focus'=>2, 'zne_status'=>2),array('order'=>'zne_id DESC'));
		$video = News::model()->getNewsData('znc_id in ('.$cat->znc_childid.') AND zne_commend=2 AND zne_status=2 AND zne_focus=1', 6);
		//最新视频
		$video_new = News::model()->getNewsData('znc_id in ('.$cat->znc_childid.') AND zne_status=2', 4);
		$video_newlist = News::model()->getNewsData('znc_id in ('.$cat->znc_childid.') AND zne_status=2', 6, 4);
		//产品宣传
		$video_pro = News::model()->getNewsData('znc_id=41 AND zne_status=2', 4);
		$video_prolist1 = News::model()->getNewsData('znc_id=41 AND zne_status=2', 4, 4);
		$video_prolist2 = News::model()->getNewsData('znc_id=41 AND zne_status=2', 4, 8);
		//其他视频
		$video_other = News::model()->getNewsData('znc_id=42 AND zne_status=2', 4);
		//热点视频
		$video_hits = News::model()->getNewsData('znc_id in ('.$cat->znc_childid.') AND zne_status=2', 5, '','zne_hits DESC');
		//商城动态
		$shop = NewsCat::model()->findByPk(1);
		$news_shop = News::model()->getNewsData('znc_id in ('.$shop->znc_childid.') AND zne_status=2', 5, '','zne_hits DESC');
		//print_r($video);

        $this->render('video', array(
            'cat'=>$cat,
            'video'=>$video,
            'video_new'=>$video_new,
            'video_newlist'=>$video_newlist,
            'video_pro'=>$video_pro,
            'video_prolist1'=>$video_prolist1,
            'video_prolist2'=>$video_prolist2,
            'video_other'=>$video_other,
            'video_hits'=>$video_hits,
            'news_shop'=>$news_shop,
            'video_focus'=>$video_focus,
        ));
	}
	
	
	public function actionDetail($id)
	{
		//设置二级导航焦点
        if ($id) {
			$cid = $id;
			$news = News::model()->findByPk($id);
			$id =$news->znc_id;
            $cat = NewsCat::model()->findByPk($news->znc_id);
			($id==9)?$this->cur='special':$news->znc_id;
        }
		//上一页
		$prev = News::model()->getNewsData('zne_id>'.$cid.' AND znc_id='.$news->znc_id.' AND zne_status=2', 1);
		//下一页
		$next = News::model()->getNewsData('zne_id<'.$cid.' AND znc_id='.$news->znc_id.' AND zne_status=2', 1);
		//相关内容
		if(isset($tag)){
			$tag = str_replace(' ', ',', $news->zt_ids);
			$news_about = News::model()->getNewsData('zt_ids in ('.$tag.') AND zne_status=2 AND znc_id='.$news->znc_id, 6, '','zne_hits DESC');
			$hotpro = Product::model()->findAll(array('condition'=>'zt_ids in ('.$tag.') AND zp_status = 2 AND zp_state = 2', 'order'=>'zp_sale_num DESC', 'limit'=>5));
		}else{
			$news_about = News::model()->getNewsData('zne_status=2 AND znc_id='.$news->znc_id,  6, '','zne_hits DESC');
			$hotpro = Product::model()->findAll(array('condition'=>'zp_status = 2 AND zp_state = 2', 'order'=>'zp_sale_num DESC', 'limit'=>5));
		}
		//精彩专题
		$subject = News::model()->getNewsData('znc_id=9 AND zne_commend=2 AND zne_status=2', 4);
		
		//点击+1
		$hits = $news->zne_hits+1;
		News::model()->updateByPk($cid,array('zne_hits'=>$hits)); 
		
		$this->render('detail', array(
            'cat'=>$cat,
            'id'=>$id,
            'news'=>$news,
            'prev'=>$prev,
            'next'=>$next,
            'news_about'=>$news_about,
            'hotpro'=>$hotpro,
            'subject'=>$subject,
        ));
    }
	
	
	
	public function actionAlbum_detail($id)
	{	
		//设置二级导航焦点
		$this->cur = 'album';
		$photo = News::model()->findByPk($id);
		
		//点击+1
		$hits = $photo->zne_hits+1;
		News::model()->updateByPk($id,array('zne_hits'=>$hits)); 
		
		$this->render('album_detail', array(
            'photo'=>$photo,
        ));
	}
	
	
	
	public function actionVideo_list()
	{
		//设置二级导航焦点
		$this->cur = 'video';
		$id = $_GET['id'];
		$cat = NewsCat::model()->findByPk(6);
		if($id == ''){ 
			$cat_c= explode(',',$cat->znc_childid);
		}else{
			$cat_c=$id;
		}
		//获取标签ID
		$tag = $_GET['tag'];
		$tags = Tag::model()->findByAttributes(array('zt_title'=>$tag));
		//获取视频子类名
		$video = NewsCat::model()->findAll(array('condition'=>'znc_id in ('.$cat->znc_childid.')'));
		
		//分页
		$criteria = new CDbCriteria;
        $criteria->compare('znc_id',$cat_c);
        $criteria->compare('zne_status', '2');
		//搜索关键词
		if($tag){
			$criteria->addcondition(" (zt_ids ='".$tags->zt_id."') or (zt_ids like '%,".$tags->zt_id."') or (zt_ids like '".$tags->zt_id.",%') or (zt_ids like '%,".$tags->zt_id.",%') ");
        }
		//($sort != '')?$criteria->order = 'zne_hits DESC':'';
        $dataProvider = new CActiveDataProvider('News', array(
            'criteria'=>$criteria,
            'pagination'=>array(
                'pageSize'=>2,
                'pageVar' => 'page',
            ),
            'sort' => array(
                'defaultOrder' => 'inputtime DESC'
            )
        ));
		//热门视频
		$video_hits = News::model()->getNewsData('znc_id in ('.$cat->znc_childid.') AND zne_status=2', 5, '','zne_hits DESC');
		
		$this->render('video_list', array(
            'dataProvider'=>$dataProvider,
            'video'=>$video,
			'video_hits'=>$video_hits,
        ));
	}
	
	
	public function actionVideo_detail($id)
	{
		//设置二级导航焦点
		$this->cur = 'video';
		$video = News::model()->findByPk($id);
		//推荐视频
		$cat = NewsCat::model()->findByPk(6);
		$video_commend = News::model()->getNewsData('znc_id in ('.$cat->znc_childid.') AND zne_commend=2 AND zne_status=2', 5);
		
		//点击+1
		$hits = $video->zne_hits+1;
		News::model()->updateByPk($id,array('zne_hits'=>$hits)); 
		
		$this->render('video_detail', array(
            'video'=>$video,
            'video_commend'=>$video_commend,
        ));
	
	}
	
	//资讯搜索
	public function actionSearch($keyword)
	{
		//分页
        $criteria = new CDbCriteria;
		//搜索关键词
		if($keyword){
			$criteria->addcondition("zne_title like '%".$keyword."%'");
        }
		$criteria->compare('zne_status', '2');
        $dataProvider = new CActiveDataProvider('News', array(
            'criteria'=>$criteria,
            'pagination'=>array(
                'pageSize'=>5,
                'pageVar' => 'page',
            ),
            'sort' => array(
                'defaultOrder' => 'zne_id DESC'
            )
        ));
		
		//右栏 专题列表
        $subject = News::model()->getNewsData('znc_id=9 AND zne_commend=2 AND zne_status=2', 4);

        $this->render('list', array(
            'dataProvider'=>$dataProvider,
            'subject'=>$subject,
        ));
	}


}